<template>
  <div class="login_container">
    <div class="login_title">瑞和大学图书馆信息管理系统</div>
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/images/user.png" alt="" />
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model.trim="loginForm.username" prefix-icon="el-icon-user-solid" placeholder="用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"
                    @keyup.enter.native="login" :show-password="true" placeholder="密码"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login" :loading="loginLoading">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <vue-particles class="login-bg" color="#39AFFD" :particleOpacity="0.7" :particlesNumber="100" shapeType="circle"
                   :particleSize="4" linesColor="#8DD1FE" :linesWidth="1" :lineLinked="true" :lineOpacity="0.4" :linesDistance="150"
                   :moveSpeed="3" :hoverEffect="true" hoverMode="grab" :clickEffect="true" clickMode="push">
    </vue-particles>

  </div>
</template>

<script>
import {getLoginData} from '../api/login/login';
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      //登录表单规则的验证对象
      loginFormRules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
      loginLoading: false
    }
  },
  methods:{
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      const data ={
        username:this.loginForm.username,
        password:this.loginForm.password
      }

      getLoginData(data).then((response)=>{
        console.log(response);
        if(response.data.token){
          // 登录成功，将Token存储在本地
          sessionStorage.setItem('token', data.token);
          // 跳转到首页或其他需要登录的页面
          localStorage.setItem('username', this.loginForm.username);
          this.$router.push('/home');
        }

      });
      // getLoginData(data).then(response =>response.json())
      // .then(data => {
      //   if (data.token) {
      //     // 登录成功，将Token存储在本地
      //     sessionStorage.setItem('token', data.token);
      //     // 跳转到首页或其他需要登录的页面
      //     this.$router.push('/home');
      //   } else {
      //     // 登录失败，提示错误信息
      //     alert('用户名或密码错误');
      //   }
      // })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url("../assets/images/login.png");
  /*将🎀页面背景  图片路径替换为你自己的图片路径*/
  background-size: cover;
  /*保持图片比例并完全覆盖元素*/
  background-position: center center;
  /*将图片居中对齐*/
}

.login_box {
  height: 300px;
  width: 450px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.login_title {
  position: relative;
  top: 5%;
  font-size: 36px;
  color: white;
  text-align: center;
  font-weight: 700;
  //控制字体间距
  letter-spacing: 10px;
}


</style>